<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'user_add.jsp' starting page</title>
    
    <link rel="stylesheet" type="text/css" href="js/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="js/themes/icon.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>
    
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript">
		$(function(){
			 $.extend($.fn.validatebox.defaults.rules, {   	
			      minLength: {   	
			          validator: function(value, param){   	
			              return value.length >= param[0];   	
			          },   	
			          message: ''  	
			      }
			  });
			 $('#code').blur(function(){
				  var value = $('#code').val();
				  document.getElementById("errorMsg").innerHTML = "正在验证.....";
				  $.ajax({
						url:'json/item_isExist.action',
						cache:false,
						data:{code:value},
						dataType:'json',
						success:function(result){
							if(result.status == "fail"){
								document.getElementById("errorMsg").innerHTML = result.msg;	
								myform.code.focus();
							} else {
								document.getElementById("errorMsg").innerHTML = result.msg;
							}
						}	
					});	
			  });		 
			  $('#grid').datagrid({   
				  	  idField:'id',
				      url:'json/item_list.action', 
				      striped:true,
				      loadMsg:'正在加载中，请等待....',
				      pagination:true,
				      fitColumns:true,
				      rownumbers:true,
				      pageSize:7,
				      pageList:[7,14,21,28,35],
				      
				      frozenColumns:[
										[{
											field:'cb',
											checkbox:true									
										 }]				               
						               ],
				      columns:[[   
				          {field:'code',title:'物料代码',width:100},   
				          {field:'name',title:'物料名称',width:100},   
				          {field:'category',title:'物料类别',width:100},   
				          {field:'pattern',title:'物料型号',width:100},  
				          {field:'spec',title:'物料规格',width:100},
				          {field:'unit',title:'计量单位',width:100,},
				          {field:'price',title:'单价',width:100,}
				      ]] ,
				      toolbar:[{
							text:'删除物料',
							iconCls:'icon-remove',
							handler:function(){
								var arr = $('#grid').datagrid('getSelections');
								if(arr.length < 1){
									$.messager.alert('提示','至少选择一个物料删除','warning');
								} else {
									 $.messager.confirm('确认','确定删除所选记录吗？',function(r){   
										       if (r){   
										           var idStr = '';
										           for(var i=0;i < arr.length;i++ ){
										        		 idStr += arr[i].id + ',';
										        	 };
										           idStr = idStr.substring(0,idStr.length-1);
										           $.post('json/item_delete.action',
										        		   {idStr:idStr},
										        		   function(){
										        			   $('#grid').datagrid('reload');
										        			   $('#grid').datagrid('clearSelections');
										        			   $.messager.show({
													        		title:'提示',
													        		height:170,
													        		msg:'删除成功'
													        	});
										        		   });
										       }   
										   }); 
								}
							}
						},{
							text:'编辑物料',
							iconCls:'icon-edit',
							handler:function(){
								var arr = $('#grid').datagrid('getSelections');
								if(arr.length == 1){
									$('#myform').get(0).reset();
									$('#dialog').dialog('open');
									$('#myform').form('load',{
										"item.id":arr[0].id,
										"item.code":arr[0].code,
										"item.name":arr[0].name,
										"item.pattern":arr[0].pattern,
										"item.category":arr[0].category,
										"item.spec":arr[0].spec,
										"item.unit":arr[0].unit,
										"item.price":arr[0].price
									});
								} else {
									$.messager.alert('提示','请选择一个用户编辑','warning'); 
								}
							}
						}]
				  });
			  $('#btn').click(function(){
				    $('#myform').form('submit', {   
					        url:'json/item_modify.action',   
					        onSubmit: function(){  
					        	if(!$('#myform').form('validate')){
					        		return false;
					        	}
					        },   
					        success:function(){  
					        	$('#dialog').dialog('close');
					        	$('#grid').datagrid('reload');
					        	$.messager.show({
					        		title:'提示',
					        		height:170,
					        		msg:'修改成功'
					        	});
					       }   
					   });
			  });
			  
			  $('#search').searchbox({   
				     width:200,   
				     searcher:function(value){   
				         $('#grid').datagrid('load',{key:value});  
				     },     
				     prompt:'请输入关键字'  
				 }); 
			  			 
			  $('#price').numberbox({   			      
				  min:0,   				     
				  precision:0,
				  required:true,
				  missingMessage:'单价必填'
			  }); 
		});
	</script>

  </head>
  
  <body>
  		关键字：<input id="search" name="key"></input>
  		<br><br><br>
		<table id="grid"></table>  	 		 	 	 	
		<div id="dialog" class="easyui-dialog" title="编辑物料" draggable=false closed=true resizable=false modal=true style="width:450px;">  	     	 
			<form id="myform" action="" method="post">
			<input type="hidden" name="item.id" value="">
		  		<table>
		  			<tr>
		  				<td>物料代码</td>
		  				<td>
		  					<input type="text" id="code" name="item.code" class="easyui-validatebox" required=true 
		  						missingMessage="物料代码必填" validType="minLength[5]" invalidMessage="物料代码至少5个字符"
		  						style="width:200px;">
		  					<font color="red"><span id="errorMsg"></span></font> 	  					
		  				</td>	  				 				
		  			</tr>
		  			<tr>
		  				<td>物料名称</td>
		  				<td>
		  					<input type="text" name="item.name" class="easyui-validatebox" required=true 
		  						missingMessage="物料名必填" validType="minLength[3]" invalidMessage="物料名至少3个字符"
		  						style="width:200px;">	  					
		  				</td>
		  			</tr>
		  			<tr>
		  				<td>物料规格</td>
		  				<td>
		  					<input type="text" name="item.spec" class="easyui-validatebox" required=true 
		  						missingMessage="物料规格不能为空" style="width:200px;">
		  				</td>
		  			</tr>
		  			<tr>
		  				<td>物料型号</td>
		  				<td>
		  					<input type="text" name="item.pattern" class="easyui-validatebox" required=true 
		  						missingMessage="物料型号不能为空" style="width:200px;">
		  				</td>
		  			</tr>
		  			<tr>
		  				<td>计量单位</td>
		  				<td>
		  					<input type="text" name="item.unit" class="easyui-validatebox" required=true 
		  						missingMessage="计量单位不能为空" style="width:200px;">
		  				</td>
		  			</tr>
		  			<tr>
		  				<td>物料类别</td>
		  				<td>
		  					 <select name="item.category" style="width:200px;" class="easyui-validatebox" 
		  					 		required=true missingMessage="物料类别不能为空">  
							      <option value="药剂">药剂</option>  					
							      <option value="药丸" selected="selected">药丸</option>  					
							      <option value="药膏">药膏</option>  											
							  </select> 
		  				</td>
		  			</tr>
		  			<tr>
		  				<td>单价</td>
		  				<td>
		  					<input id="price" type="text" name="item.price" style="width:200px;">
		  				</td>
	  				</tr>
		  			<tr>
		  				<td></td>
		  				<td>
		  					<a id="btn" class="easyui-linkbutton">保存</a>
		  				</td>
		  			</tr>
		  		</table>	
		  	</form>	
		  </div> 
  	
  </body>
</html>
